export
const format = function (unixStamp) { // unixStamp 精确到微秒
  var _today_obj = new Date(),
    _today_date = {
      y : _today_obj.getFullYear(),
      m : ( _today_obj.getMonth() + 1 < 10 ? '0' + ( _today_obj.getMonth() - - 1 ) : (_today_obj.getMonth() - - 1) ),
      d : ( _today_obj.getDate() < 10 ? '0' + _today_obj.getDate() : _today_obj.getDate() )
    }
  var _today_stamp = Date.parse(_today_date.y + '/' + _today_date.m + '/' + _today_date.d + ' 00:00:00')

  var stamp = []
  stamp[0] = _today_stamp + 86400000
  stamp[1] = _today_stamp
  stamp[2] = _today_stamp - 86400000
  stamp[3] = _today_stamp - 172800000
  stamp[4] = _today_stamp - 518400000 // 7天
  stamp[5] = _today_stamp - 31536000000 // 365天

  var _compare_obj = new Date()
  _compare_obj.setTime(unixStamp)

  var returnStr

  if (unixStamp >= stamp[1] && unixStamp < stamp[0]) {
    returnStr = _compare_obj.getHours() + ':' +  ( _compare_obj.getMinutes() < 10 ? '0' + _compare_obj.getMinutes() : _compare_obj.getMinutes() )
  } else if (unixStamp >= stamp[2] && unixStamp < stamp[1]) {
    var yesterdayText = '昨天'
    returnStr = yesterdayText  + ' ' + _compare_obj.getHours() + ':' +          ( _compare_obj.getMinutes() < 10 ? '0' + _compare_obj.getMinutes() : _compare_obj.getMinutes() )
  } else if (unixStamp >= stamp[3] && unixStamp < stamp[2]) {
    var theDayBeforeYesterdayText = '前天'
    returnStr = theDayBeforeYesterdayText  +  ' ' + _compare_obj.getHours() + ':' +          ( _compare_obj.getMinutes() < 10 ? '0' + _compare_obj.getMinutes() : _compare_obj.getMinutes() )

  } else if (unixStamp >= stamp[4] && unixStamp < stamp[3]) { // 7天内
    var daynames = ['天', '一', '二', '三', '四', '五', '六']
    var dathStr = '星期' + daynames[_compare_obj.getDay()]

    var SundayText = '星期天'
    var MondayText = '星期一'
    var TuesdayText = '星期二'
    var WednesdayText = '星期三'
    var ThursdayText = '星期四'
    var FridayText = '星期五'
    var SaturdayText = '星期六'

    var dathStr2

    switch (dathStr) {
      case '星期天':
        dathStr2 = SundayText
        break
      case '星期一':
        dathStr2 = MondayText
        break
      case '星期二':
        dathStr2 = TuesdayText
        break
      case '星期三':
        dathStr2 = WednesdayText
        break
      case '星期四':
        dathStr2 = ThursdayText
        break
      case '星期五':
        dathStr2 = FridayText
        break
      case '星期六':
        dathStr2 = SaturdayText
        break
      default:
        dathStr2 = dathStr
        break
    }

    returnStr = dathStr2 + ' ' + _compare_obj.getHours() + ':' +          ( _compare_obj.getMinutes() < 10 ? '0' + _compare_obj.getMinutes() : _compare_obj.getMinutes() )
  } else if (unixStamp >= stamp[5] && unixStamp < stamp[4]) { // 365天内
    var monthText = '月'
    var dayText = '日'
    returnStr = (_compare_obj.getMonth() - (-1)) + monthText + _compare_obj.getDate() + dayText + ' '
      + _compare_obj.getHours() + ':' +  ( _compare_obj.getMinutes() < 10 ? '0' + _compare_obj.getMinutes() : _compare_obj.getMinutes() )

  } else {
    var yearText = '年'
    var monthText = '月'
    var dayText = '日'
    returnStr = _compare_obj.getFullYear() + yearText + (_compare_obj.getMonth() - (-1)) +          monthText + _compare_obj.getDate() + dayText + ' ' + _compare_obj.getHours() + ':' +
      ( _compare_obj.getMinutes() < 10 ? '0' + _compare_obj.getMinutes() : _compare_obj.getMinutes() )
  }
  return returnStr
};
const numFilter = function (value) {
  // 截取当前数据到小数点后两位
  if (value == null || value == "" || value == undefined || value == 'NAN') {
    let val = parseFloat(0).toFixed(2);
    return val;
  } else {
    let realVal = parseFloat(value).toFixed(2);
    return realVal;
  }
};
const filterType = function(val){
  if(val === 0) return "0 B"
  var k = 1024;
  var sizes = ['B','KB','MB','GB','PB','TB','EB','ZB','YB'],
    i = Math.floor(Math.log(val) / Math.log(k));
  return (val / Math.pow(k,i)).toPrecision(3) + "" + sizes[i]
};
const timeChange = function (timestamp,mustIncludeTime) {//发送聊天记录发送时间
  var currentDate = new Date();
  // 目标判断时间
  var srcDate = new Date(parseInt(timestamp));
  var currentYear = currentDate.getFullYear();
  var currentMonth = (currentDate.getMonth()+1);
  var currentDateD = currentDate.getDate();
  var srcYear = srcDate.getFullYear();
  var srcMonth = (srcDate.getMonth()+1);
  var srcDateD = srcDate.getDate();
  var ret = "";
  // 要额外显示的时间分钟
  let _formatDate = function(date, fmt) {
    var o = {
      "M+": date.getMonth() + 1, //月份
      "d+": date.getDate(), //日
      "h+": date.getHours(), //小时
      "m+": date.getMinutes(), //分
      "s+": date.getSeconds(), //秒
      "q+": Math.floor((date.getMonth() + 3) / 3), //季度
      "S": date.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
      if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
  };
  // var timeExtraStr = _formatDate(srcDate, "hh:mm");
  var timeExtraStr = (mustIncludeTime?" "+_formatDate(srcDate, "hh:mm"):"");
  // 当年
  if(currentYear == srcYear) {
    var currentTimestamp = currentDate.getTime();
    var srcTimestamp = timestamp;
    // 相差时间（单位：毫秒）
    var deltaTime = (currentTimestamp-srcTimestamp);
    // 当天（月份和日期一致才是）
    if(currentMonth == srcMonth && currentDateD == srcDateD) {
      // 时间相差60秒以内
      if(deltaTime < 60 * 1000)
        ret = "刚刚";
      // 否则当天其它时间段的，直接显示“时:分”的形式
      else
        ret = _formatDate(srcDate, "hh:mm");
    }
    // 当年 && 当天之外的时间（即昨天及以前的时间）
    else {
      // 昨天（以“现在”的时候为基准-1天）
      var yesterdayDate = new Date();
      yesterdayDate.setDate(yesterdayDate.getDate()-1);
      // 前天（以“现在”的时候为基准-2天）
      var beforeYesterdayDate = new Date();
      beforeYesterdayDate.setDate(beforeYesterdayDate.getDate()-2);
      // 用目标日期的“月”和“天”跟上方计算出来的“昨天”进行比较，是最为准确的（如果用时间戳差值
      // 的形式，是不准确的，比如：现在时刻是2019年02月22日1:00、而srcDate是2019年02月21日23:00，
      // 这两者间只相差2小时，直接用“deltaTime/(3600 * 1000)” > 24小时来判断是否昨天，就完全是扯蛋的逻辑了）
      if(srcMonth == (yesterdayDate.getMonth()+1) && srcDateD == yesterdayDate.getDate())
        ret = "昨天"+timeExtraStr;// -1d
      // “前天”判断逻辑同上
      else if(srcMonth == (beforeYesterdayDate.getMonth()+1) && srcDateD == beforeYesterdayDate.getDate())
        ret = "前天"+timeExtraStr;// -2d
      else{
        // 跟当前时间相差的小时数
        var deltaHour = (deltaTime/(3600 * 1000));
        // 如果小于或等 7*24小时就显示星期几
        if (deltaHour <= 7*24){
          var weekday=new Array(7);
          weekday[0]="星期日";
          weekday[1]="星期一";
          weekday[2]="星期二";
          weekday[3]="星期三";
          weekday[4]="星期四";
          weekday[5]="星期五";
          weekday[6]="星期六";
          // 取出当前是星期几
          var weedayDesc = weekday[srcDate.getDay()];
          ret = weedayDesc+timeExtraStr;
        }
        // 否则直接显示完整日期时间
        else
          ret = _formatDate(srcDate, "yyyy/M/d")+timeExtraStr;
      }
    }
  }
  // 往年
  else{
    ret = _formatDate(srcDate, "yyyy/M/d")+timeExtraStr;
  }
  return ret;
};
const formatStatus = val => val === "" ? "-" : val === null ? "-" :val === 'null'?'-': val;


import moment from 'moment'
const momentFilter =  (input, fmtstring) => {
  // 使用momentjs这个日期格式化类库实现日期的格式化功能
  return moment(input).format(fmtstring);
}


  export default {
  install(Vue) {
    Vue.filter('numFilter', numFilter);
    Vue.filter('filterType', filterType);
    Vue.filter('format', format);
    Vue.filter('timeChange', timeChange);
    Vue.filter('formatStatus', formatStatus);
    Vue.filter('moment', momentFilter);
  }
};
